<template>
  <el-dialog :title="formData.seriesUuid ? '修改' : '添加'" :model-value="dialog" @close="close">
    <el-form :model="formData" :rules="rules" ref="uploadForm" label-width="auto" style="padding: 0 20px" class="Dform">
      <el-form-item label="品牌：" prop="brandUuid">
        <el-select v-model="formData.brandUuid" filterable clearable style="width: 100%" placeholder="请选择品牌">
          <el-option v-for="item in brandList" :key="item.brandUuid" :label="item.brandName" :value="item.brandUuid" />
        </el-select>
      </el-form-item>
      <el-form-item label="车系名称：" prop="seriesName">
        <el-input v-model="formData.seriesName" />
      </el-form-item>
      <el-form-item>
        <el-button type="primary" @click="onSubmit()">提交</el-button>
        <el-button v-if="!formData.seriesUuid" @click="resetForm()">重置</el-button>
      </el-form-item>
    </el-form>
  </el-dialog>
</template>

<script lang="ts">
import { defineComponent, ref, watch, reactive } from 'vue'
import { ElMessage } from 'element-plus'
import { updateSeries, saveSeries, brandQueryAll } from '@/api/vehicle'
import iconSelect from '@/components/icon-select/index.vue'

export default defineComponent({
  components: {
    iconSelect,
  },
  props: {
    dialog: {
      type: Boolean,
      default: false,
    },
    currentForm: {
      type: Object,
      default: () => { },
    },
  },
  emits: ['update:currentForm', 'update:dialog', 'refresh'],
  setup(props, context) {
    const formData = reactive(props.currentForm)
    const uploadForm = ref<any>(null)
    const brandList = ref<any>([])


    watch(() => props.dialog,
      (val) => {
        Object.assign(formData, props.currentForm)
      }
    )

    const close = () => {
      context.emit('update:dialog', false)
      context.emit('refresh', { ...formData })
      context.emit('update:currentForm', {})
    }

    const onSubmit = () => {
      uploadForm.value.validate((valid: boolean) => {
        if (valid) {
          if (formData.seriesUuid) {
            updateSeries(formData).then((res: any) => {
              if (res.code == 200) {
                ElMessage.success('修改成功')
                close()
              }
            })
          } else {
            saveSeries(formData).then((res: any) => {
              if (res.code == 200) {
                ElMessage.success('添加成功')
                close()
              }
            })
          }

        } else {
          ElMessage({
            message: '验证失败',
            type: 'error',
            duration: 1000,
          })
          return false
        }
      })
    }

    onMounted(() => {
      brandQueryAll().then((res: any) => {
        brandList.value = res.data || []
      })
    })

    const resetForm = () => {
      uploadForm.value.resetFields()
      context.emit('update:currentForm', {})
    }

    return {
      uploadForm,
      formData,
      close,
      brandList,
      resetForm,
      onSubmit,
      rules: {
        brandUuid: [
          { required: true, message: '请选择品牌', trigger: 'change' }
        ],
        seriesName: [
          { required: true, message: '请输入车系名称', trigger: 'blur' }
        ],
      },
    }
  },
})
</script>

<style></style>
